package com.cd.dao;

import com.cd.entity.TVCategory;
import com.cd.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.List;

public class TVCategoryDao {
    // 1. 查询所有电视剧分类
    public List<TVCategory> findAll() throws IOException {
        try (SqlSession session = MybatisUtil.getSqlSession(true)) {
            return session.selectList("com.cd.dao.TVCategoryMapper.findAll");
        }
    }

    // 2. 根据ID查询分类
    public TVCategory findById(Integer categoryId) throws IOException {
        try (SqlSession session = MybatisUtil.getSqlSession(true)) {
            return session.selectOne("com.cd.dao.TVCategoryMapper.findById", categoryId);
        }
    }

    // 3. 插入分类
    public int insert(TVCategory category) throws IOException {
        try (SqlSession session = MybatisUtil.getSqlSession(true)) {
            return session.insert("com.cd.dao.TVCategoryMapper.insert", category);
        }
    }

    // 4. 更新分类
    public int update(TVCategory category) throws IOException {
        try (SqlSession session = MybatisUtil.getSqlSession(true)) {
            return session.update("com.cd.dao.TVCategoryMapper.update", category);
        }
    }

    // 5. 删除分类
    public int delete(Integer categoryId) throws IOException {
        try (SqlSession session = MybatisUtil.getSqlSession(true)) {
            return session.delete("com.cd.dao.TVCategoryMapper.delete", categoryId);
        }
    }
}